package com.appshelves.model.saveable;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public abstract class Saveable {

    private Object tag;

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public Object getTag() {
        return tag;
    }

    protected abstract void readFromStream(DataInputStream stream) throws IOException;

    protected abstract void writeToStream(DataOutputStream stream) throws IOException;

    protected void initOnLoading(Object arg) {

    }

    protected static <T extends Saveable> T createFromStream(DataInputStream stream, Class<T> cls, Object arg)
        throws IOException {

        try {
            T saveable = cls.newInstance();
            saveable.initOnLoading(arg);
            saveable.readFromStream(stream);
            return saveable;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /// debug

    protected void log() {

    }
}
